package com.xs.web;

import com.xs.UserService;
import com.xs.dao.UserDao;
import com.xs.dbs.DataSourceContextHolder;
import com.xs.dbs.DatabaseType;
import com.xs.domain.User;
import com.xs.util.PageInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xs
 * date 2019-04-29 11:03
 */
@RestController
public class UserController {

    @Autowired
    UserDao userDao;

    @Autowired
    UserService userService;


    @GetMapping("/listUser")
    public List<User> listUser(DatabaseType databaseType) {
        DataSourceContextHolder.setDataSourceKey(databaseType);
        List<User> userList = userDao.select(new User());
        return userList;

    }

    @GetMapping("/testAsyncQuery")
    public String testAsyncQuery(DatabaseType databaseType) {
        DataSourceContextHolder.setDataSourceKey(databaseType);
        userService.testAsyncQuery();
        return "ok";
    }

    @GetMapping("/testTransaction")
    public String testTransaction(DatabaseType databaseType, Boolean needThrowExp) {
        DataSourceContextHolder.setDataSourceKey(databaseType);
        userService.testTransaction(needThrowExp);
        return "ok";
    }


    @GetMapping("/testPage")
    public PageInfoDto testPage(DatabaseType databaseType, Integer pageCurrent, Integer pageSize) {
        DataSourceContextHolder.setDataSourceKey(databaseType);
        return userService.testPage(pageCurrent, pageSize);
    }

}
